home *** CD-ROM | disk | FTP | other *** search
/ Java 1996 August / Java - Summer 1996.iso / kaffe-0.2 / lib / native / java.io / java.io.FileOutputStream.c < prev    next >
C/C++ Source or Header  |  1996-02-14  |  2KB  |  85 lines

  1. /*
  2.  * java.io.FileOutputStream.c
  3.  *
  4.  * Copyright (c) 1996 Systems Architecture Research Centre,
  5.  *           City University, London, UK.
  6.  *
  7.  * See the file "license.terms" for information on usage and redistribution
  8.  * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  9.  *
  10.  * Written by Tim Wilkinson <tim@sarc.city.ac.uk>, February 1996.
  11.  */
  12.  
  13. #include <stdio.h>
  14. #include <assert.h>
  15. #include "files.h"
  16. #include "java.io.FileOutputStream.h"
  17. #include "java.io.FileDescriptor.h"
  18.  
  19. /*
  20.  * Open a file for output.
  21.  */
  22. void
  23. java_io_FileOutputStream_open(struct Hjava_io_FileOutputStream* fh, struct Hjava_lang_String* nm)
  24. {
  25.     int fd;
  26.     char str[MAXPATHLEN];
  27.  
  28.     javaString2CString(nm, str, sizeof(str));
  29.  
  30.     fd = open(str, O_WRONLY|O_CREAT, 0666);
  31.     unhand(unhand(fh)->fd)->fd = fd;
  32.     if (fd < 0) {
  33.         SignalError(0, "java.io.IOException", SYS_ERROR);
  34.     }
  35. }
  36.  
  37. /*
  38.  * Close a file.
  39.  */
  40. void
  41. java_io_FileOutputStream_close(struct Hjava_io_FileOutputStream* fh)
  42. {
  43.     int r;
  44.  
  45.     if (unhand(unhand(fh)->fd)->fd >= 0) {
  46.         r = close(unhand(unhand(fh)->fd)->fd);
  47.         unhand(unhand(fh)->fd)->fd = -1;
  48.         if (r < 0) {
  49.             SignalError(0, "java.io.IOException", SYS_ERROR);
  50.         }
  51.     }
  52. }
  53.  
  54. /*
  55.  * Write bytes to file.
  56.  */
  57. void
  58. java_io_FileOutputStream_writeBytes(struct Hjava_io_FileOutputStream* fh, HArray* byteArray, long start, long len)
  59. {
  60.     int fd;
  61.     int r;
  62.  
  63.     fd = unhand(unhand(fh)->fd)->fd;
  64.     r = write(fd, &byteArray->data[start], len);
  65.     if (r < 0) {
  66.         SignalError(0, "java.io.IOException", SYS_ERROR);
  67.     }
  68. }
  69.  
  70. /*
  71.  * Write a byte to file.
  72.  */
  73. void
  74. java_io_FileOutputStream_write(struct Hjava_io_FileOutputStream* fh, long byte)
  75. {
  76.     int fd;
  77.     int r;
  78.  
  79.     fd = unhand(unhand(fh)->fd)->fd;
  80.     r = write(fd, &byte, 1);
  81.     if (r < 0) {
  82.         SignalError(0, "java.io.IOException", SYS_ERROR);
  83.     }
  84. }
  85.